﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace FileUtil
{
    class common
    {
        private static void WriteDebugMessage(string message)
        {
#if DEBUG
            Console.WriteLine(message);
#endif
        }
        public static string GetExtName(string filePath)
        {
            return Path.GetExtension(filePath);
        }
        public static string GetOnlyFileName(string filePath)
        {
            return Path.GetFileNameWithoutExtension(filePath);
        }
        public static string GetFileName(string filePath)
        {
            return Path.GetFileName(filePath);
        }
        public static string GetFileDirectory(string filePath)
        {
            return Path.GetDirectoryName(filePath);
        }
        public static string GetOnlyFileDirectory(string filePath)
        {
            string directory = GetFileDirectory(filePath);
            string drive = GetDrive(filePath);
            return directory.Substring(drive.Length - 1);
        }
        public static string GetDrive(string filePath)
        {
            return Path.GetPathRoot(filePath);
        }
        public static bool IsExistFile(string filePath)
        {
            bool bRet = false;
            FileInfo fileInfo = null;
            try
            {
                fileInfo = new FileInfo(filePath);
                bRet = fileInfo.Exists;
            }
            catch (Exception ex)
            {
                WriteDebugMessage(ex.ToString());
                bRet = false;
            }
            return bRet;
        }
        public static bool IsExistDirectory(string filePath)
        {
            bool bRet = false;
            DirectoryInfo directoryInfo = null;
            try
            {
                directoryInfo = new DirectoryInfo(filePath);
                bRet = directoryInfo.Exists;
            }
            catch (Exception ex)
            {
                WriteDebugMessage(ex.ToString());
                bRet = false;
            }
            return bRet;
        }

        public static bool MakeDirectory(string filePath)
        {
            bool bRet = false;
            DirectoryInfo directoryInfo = null;
            try
            {
                directoryInfo = new DirectoryInfo(filePath);
                if (!directoryInfo.Exists)
                    directoryInfo.Create();

                bRet = directoryInfo.Exists;
            }
            catch (Exception ex)
            {
                WriteDebugMessage(ex.ToString());
                bRet = false;
            }
            return bRet;
        }
        // .net 4.0(Visual Studio 2010(버전부터 optional parameter
        public static bool rmDirectory(string filePath, bool flag = false)
        {
            bool bRet = false;
            DirectoryInfo directoryInfo = null;
            try
            {
                directoryInfo = new DirectoryInfo(filePath);
                directoryInfo.Delete(flag);
                bRet = IsExistDirectory(filePath);
            }
            catch (Exception ex)
            {
                WriteDebugMessage(ex.ToString());
                bRet = false;
            }
            return bRet;
        }

        public static bool readFileList(string filePath, ref List<string> contents, Encoding encodeType)
        {
            if (!IsExistFile(filePath))
                return false;

            bool bRet = false;

            System.IO.StreamReader reader = null;

            try
            {
                reader = new System.IO.StreamReader(filePath, encodeType);
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    contents.Add(line);
                }
                bRet = true;

            }
            catch (Exception ex)
            {
                WriteDebugMessage(ex.ToString());
                bRet = false;
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
            }
            return bRet;
        }
        public static bool WriteFileList(string filePath, ref List<string> contents, Encoding encodeType, bool folderMode = true, bool appendMode = false)
        {
            bool bRet = false;

            if (folderMode && !IsExistDirectory(GetFileDirectory(filePath)))
            {
                MakeDirectory(GetFileDirectory(filePath));
            }

            System.IO.StreamWriter writer = null;

            try
            {
                writer = new System.IO.StreamWriter(filePath, appendMode, encodeType);

                foreach (string content in contents)
                {
                    writer.WriteLine(content);
                }

                if (IsExistFile(filePath))
                    bRet = true;
            }
            catch (Exception ex)
            {
                WriteDebugMessage(ex.ToString());
                bRet = false;
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                    writer.Dispose();
                }
            }

            return bRet;
        }
        public static bool excuteCommandLine(string cmdLine, Int32 timeout = 5)
        {
            bool bRet = false;

            Process process = null;

            try
            {
                process = new Process();

                process.StartInfo.UseShellExecute = false;
                process.StartInfo.FileName = cmdLine;
                process.StartInfo.CreateNoWindow = true;
                process.Start();
                process.WaitForInputIdle();
                if (timeout != 0)
                    process.WaitForExit(timeout * 60 * 1000);
                else
                    process.WaitForExit();

                if (process.HasExited == false)
                {
                    int nRet = process.ExitCode;
                    WriteDebugMessage("exe return value == " + nRet);
                    if (nRet == 0) bRet = true;
                }
                else
                {
                    if (process.Responding)
                        process.CloseMainWindow();
                    else
                        process.Kill();
                }
            }
            catch (Exception ex)
            {
                WriteDebugMessage(ex.ToString());
                bRet = false;
            }
            finally
            {
                if (process != null)
                {
                    process.Close();
                    process.Dispose();
                }
            }
            return bRet;
        }

        public static bool CoryFolder(string srcPath, string dstPath)
        {
            bool bRet = true;
            if (!IsExistDirectory(dstPath))
            {
                MakeDirectory(dstPath);
            }

            if (bRet)
            {
                string[] files = Directory.GetFiles(srcPath);
                string[] folders = Directory.GetDirectories(srcPath);

                try
                {
                    foreach (string folder in folders)
                    {
                        string tmpSrcFolder = folder;
                        string tmpDstFolder = Path.Combine(dstPath, GetFileName(folder));
                        //string tmpDstFolder = dstPath + "\\" + GetFileName(folder);

                        bRet = CoryFolder(tmpSrcFolder, tmpDstFolder);
                        if (bRet) break;
                    }
                }
                catch (Exception ex)
                {
                    WriteDebugMessage(ex.ToString());
                    bRet = false;
                }

                try
                {
                    foreach (string file in files)
                    {
                        string tmpSrcFile = file;
                        string tmpDstFile = Path.Combine(dstPath, GetFileName(file));

                        File.Copy(tmpSrcFile, tmpDstFile);
                    }
                }
                catch (Exception ex)
                {
                    WriteDebugMessage(ex.ToString());
                    bRet = false;
                }
            }
            return bRet;
        }

        public static string GetModuleFileName()
        {
            return System.Reflection.Assembly.GetExecutingAssembly().Location;
        }
    }
}